package com.gwclh.personnel.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gwclh.personnel.bean.ResultVo;
import com.gwclh.personnel.domain.PageDomain;
import com.gwclh.personnel.entity.Employee;
import com.gwclh.personnel.entity.Station;
import com.gwclh.personnel.mapper.StationMapper;
import com.gwclh.personnel.service.IEmployeeService;
import com.gwclh.personnel.service.IStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwclh.personnel.vo.stationvo.StationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 岗位表
 * 服务实现类
 * </p>
 *
 * @author gzhhh
 * @since 2020-07-24
 */
@Service
//@Slf4j
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements IStationService {
    @Autowired
    IEmployeeService employeeService;

    @Override
    public ResultVo insertSta(Station station) {
        QueryWrapper<Station> stationQueryWrapper = new QueryWrapper<>();
        stationQueryWrapper.eq("sta_name", station.getStaName());
        Assert.isNull(this.getOne(stationQueryWrapper), "该岗位已存在");
        Assert.isTrue(this.save(station), "添加岗位失败");
        return ResultVo.success();
    }

    @Override
    public ResultVo delSta(Integer id) {
        QueryWrapper <Employee>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("sta_id",id);
        int countStaId = employeeService.count(queryWrapper);
        if(countStaId>0){
            return ResultVo.fail("删除失败，当前岗位还有员工");
        }else
            Assert.isTrue(this.removeById(id),"删除岗位失败");
            return ResultVo.success();
    }

    @Override
    public ResultVo listStations(PageDomain pageDomain, Station station) {
        Page<Station> stationPage = new Page<Station>(pageDomain.getPageNum(), pageDomain.getPageSize());
        stationPage.addOrder(pageDomain.getOrderItemBy());
        QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("sta_name", station.getStaName());
        if (station.getStaType() != null) {
            queryWrapper.eq("sta_type", station.getStaType());
        }
        this.page(stationPage, queryWrapper);
        List<StationVo> stationVos = new ArrayList<>();

        stationPage.getRecords().forEach(e -> {
            StationVo stationVo = new StationVo();
            BeanUtils.copyProperties(e, stationVo);
            stationVo.setCountStaId(employeeService.count(new QueryWrapper<Employee>().eq("sta_id", e.getId())));
            stationVos.add(stationVo);
        });
        HashMap<String, Object> result = new HashMap<>();

        result.put("stations", stationVos);
        result.put("total", stationPage.getTotal());
        return ResultVo.success(result);
    }
}
